package 周赛;

public class 给植物浇水 {
    public int wateringPlants(int[] plants, int capacity) {
        int res = 0;
        boolean flag = false;
        int tem = capacity;
        for (int i = 0; i < plants.length-1; i++) {
            if (flag)
                res+=1;
            else
               res+=(i+1);
            tem-=plants[i];
            if (tem>=plants[i+1]){
                flag = true;
                continue;
            }
            else{
                res+=(i+1);
                tem = capacity;
                flag = false;
            }
        }
        //最后一次判断
        if (flag)
            res+=1;
        else
            res+=plants.length;
        return res;
    }
}
